package com.wisnews.pojo.sec.vo;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.wisnews.pojo.sr.entity.News;
import com.wisnews.service.sec.Impl.NewsServiceImpl;
import com.wisnews.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
*@Description：自定义Pipeline 把爬虫数据写入数据库
*@Author: yuyd
*@Email: yyd8358@foxmail.com
*@Date: 2020/12/25 16:07
*@Version 1.0
*/
@Component
@Slf4j
public class NewsFor163Pipeline implements Pipeline {

    @Autowired
    private NewsServiceImpl newsService;

    /**
     * @description: resultItems 中存储了，我们爬取的map 解析数据成实体bean 存入数据库
     * @param:
     * @return:
     * @author yuyd
     * @date: 2021/3/12 17:13
     */
    @Override
    public void process(ResultItems resultItems, Task task) {

        StringBuilder stringBuilder = new StringBuilder("");
        log.info("==》将爬虫信息写入数据库!");
        Map<String, Object> all = resultItems.getAll();

        News news = new News();
        List newsContent = (List) all.get("newsContent");
        if (newsContent !=null) {
            newsContent.forEach(onenews -> {
                stringBuilder.append(onenews);
            });
            news.setNewsContent(stringBuilder.toString());
        }

        Object newsTitle = all.get("newsTitle");
        Object newsType = all.get("newsType");
        Object newsAnnounceTime1 = all.get("newsAnnounceTime");
        Object newsMedia = all.get("newsMedia");
        Object newsFromUrl = all.get("newsFromUrl");
        news.setNewsFromUrl(newsFromUrl.toString());
        if (newsTitle != null) {
            news.setNewsTitle(newsTitle.toString());
        }
        if (newsType != null) {
            news.setNewsType(newsType.toString());
        }
        String media = (String)all.get("media");
        if (StringUtils.isNotBlank(media)) {
            news.setNewsMedia(media);
        }
        if (newsAnnounceTime1 != null){
            String s = String.valueOf(newsAnnounceTime1);
            log.error(s);
        String newsAnnounceTime = s.substring(1, 20);
        news.setNewsAnnounceTime(newsAnnounceTime);
            news.setNewsMedia(s.split("来源:")[1]);
        }

        String context = stringBuilder.toString();
        int begin = context.indexOf("https://nimg.ws.126.net/");
        int end = context.indexOf("type=jpg");
        //必须要有图片 否则数组越界异常
        if (begin<end){
            news.setNewsHomeImgurl(context.substring(begin, end + 8));
        }

        //保存获取时间
        LocalDateTime localDateTime = DateUtil.getLocalDateTime();
        news.setCreateTime(localDateTime);
        newsService.save(news);
        log.info("《==写入数据库完成");
    }
}
